rm(list = ls())


agg_data <- read.csv(
  file = "aggregated_data.csv",
  header = TRUE,
  sep = ";",
  dec = ","
)




packages <- c("ggplot2", "plyr", "dplyr", "lavaan", "cowplot", "rmarkdown", 
              "readr", "caTools", "bitops", "ggpubr")

if (length(setdiff(packages, rownames(installed.packages()))) > 0) {
  install.packages(setdiff(packages, rownames(installed.packages())))  
}

library(cowplot)
library(dplyr)
library(readr)
library(ggpubr)
theme_set(theme_pubr())


source("R_rainclouds.R")
source("summarySE.R")
#source("simulateData.R")

# width and height variables for saved plots
w = 6
h = 6


agg_data$cam<-as.factor(agg_data$cam)
agg_data$cam<- factor(agg_data$cam,
                      levels = c(0,1),
                      labels = c("RAM", "CAM"))


agg_data$pi<-as.factor(agg_data$pi)
agg_data$pi<- factor(agg_data$pi,
                     levels = c(0,1),
                     labels = c("No Info", "Perfect Info"))



str(agg_data)

##### -------- AGGREGATED RESULTS -------- ####


##### OUTPUT ####

agg_data$ni<-factor(agg_data$pi,levels=levels(agg_data$pi)[c(2, 1)])

str(agg_data)

p7 <- ggplot(agg_data, aes(x = ni, y =avoutput, fill = cam)) +
  geom_flat_violin(aes(fill = cam),position = position_nudge(x = 0.1, y = 0), adjust = 0.7, trim = TRUE, alpha = .7, colour = NA)+
  geom_point(aes(x = as.numeric(ni)-.15, y = avoutput, colour = cam),position = position_jitter(width = .05), size = 3, shape = 20)+
  geom_boxplot(aes(x = ni, y = avoutput, fill = cam),outlier.shape = NA, alpha = .8, width = .15, colour = "black")+
  #geom_line(data = sumrepdat7, aes(x = as.numeric(pi)+0.1, y = avoutput_mean, group = cam, colour = cam), linetype = 3)+
  #geom_point(data = sumrepdat7, aes(x = as.numeric(pi)+0.1, y = avoutput_mean, group = cam, colour = cam), shape = 18) +
  #geom_errorbar(data = sumrepdat7, aes(x = as.numeric(pi)+0.1, y = avoutput_mean, group = cam, colour = cam, ymin = avoutput_mean-se, ymax = avoutput_mean+se), width = .05)+
  scale_y_continuous(breaks = seq(0, 100, 10)) +
  theme_minimal() +
  theme(axis.title.y = element_blank()) +
  theme(axis.title.x = element_blank()) +
  theme(legend.title = element_blank()) +
  theme(legend.position="bottom") +
  #ylab("Activity level") +
  coord_flip() +
  theme(legend.title = element_blank()) +
  scale_fill_manual(values=c("#FF0000", "#0000FF")) +
  scale_color_manual(values=c("#FF0000", "#0000FF")) +
  ggtitle("Emissions") 

#p7 + scale_fill_discrete(name = "Audit Mechanism") +
#  scale_color_discrete(name = "Audit Mechanism")


p7

ggsave('agg_output_scatter.eps')



#### REPORT

p8 <- ggplot(agg_data, aes(x = ni, y =avreport, fill = cam)) +
  geom_flat_violin(aes(fill = cam),position = position_nudge(x = 0.1, y = 0), adjust = 0.7, trim = TRUE, alpha = .7, colour = NA)+
  geom_point(aes(x = as.numeric(ni)-.15, y = avreport, colour = cam),position = position_jitter(width = .05), size = 3, shape = 20)+
  geom_boxplot(aes(x = ni, y = avreport, fill = cam),outlier.shape = NA, alpha = .8, width = .15, colour = "black")+
  #geom_line(data = sumrepdat7, aes(x = as.numeric(pi)+0.1, y = avoutput_mean, group = cam, colour = cam), linetype = 3)+
  #geom_point(data = sumrepdat7, aes(x = as.numeric(pi)+0.1, y = avoutput_mean, group = cam, colour = cam), shape = 18) +
  #geom_errorbar(data = sumrepdat7, aes(x = as.numeric(pi)+0.1, y = avoutput_mean, group = cam, colour = cam, ymin = avoutput_mean-se, ymax = avoutput_mean+se), width = .05)+
  scale_y_continuous(breaks = seq(0, 100, 10)) +
  theme_minimal() +
  theme(axis.title.y = element_blank()) +
  theme(axis.title.x = element_blank()) +
  theme(legend.title = element_blank()) +
  theme(legend.position="bottom") +
  #ylab("Activity level") +
  coord_flip() +
  theme(legend.title = element_blank()) +
  scale_fill_manual(values=c("#FF0000", "#0000FF")) +
  scale_color_manual(values=c("#FF0000", "#0000FF")) +
  ggtitle("Reported Emissions") 

#p7 + scale_fill_discrete(name = "Audit Mechanism") +
#  scale_color_discrete(name = "Audit Mechanism")


p8


ggsave('agg_report_scatter.eps')



figure <- ggarrange(p7, p8,
                    #labels = c("A", "B"),
                    ncol = 1, nrow = 2,
                    common.legend = TRUE, legend = "bottom")
figure

ggsave(figure, height = 7 , width = 7, filename = 'agg_results.pdf')



##### -------- MAIN RESULTS -------- ####


rm(list = ls())


full_data <- read.csv(
  file = "data_per_round.csv",
  header = TRUE,
  sep = ";",
  dec = ","
)



packages <- c("ggplot2", "plyr", "dplyr", "lavaan", "cowplot", "rmarkdown", 
              "readr", "caTools", "bitops")

if (length(setdiff(packages, rownames(installed.packages()))) > 0) {
  install.packages(setdiff(packages, rownames(installed.packages())))  
}

library(cowplot)
library(dplyr)
library(readr)

source("R_rainclouds.R")
source("summarySE.R")
#source("simulateData.R")



####### FULL DATA ######
full_data$round<-as.factor(full_data$round)
full_data$round<- factor(full_data$round,
                         levels = c(1,2,3,4,5,6,7),
                         labels = c("round 1", "round 2", "round 3", "round 4", "round 5", "round 6", "round 7"))



full_data$cam<-as.factor(full_data$cam)
full_data$cam<- factor(full_data$cam,
                       levels = c(0,1),
                       labels = c("RAM", "CAM"))


full_data$pi<-as.factor(full_data$pi)
full_data$pi<- factor(full_data$pi,
                      levels = c(0,1),
                      labels = c("No Info", "Perfect Info"))

str(full_data)


ni_data <- subset(full_data, pi=="No Info")
pi_data <- subset(full_data, pi=="Perfect Info")




### OUTPUT NI ####

sumrepdat <- summarySE(ni_data, measurevar = "output", groupvars=c("cam", "round"))

head(sumrepdat)
str(sumrepdat)


p1 <- ggplot(ni_data, aes(x = round, y =output, fill = cam)) +
  #geom_flat_violin(aes(fill = cam),position = position_nudge(x = 0, y = 0), adjust = 0.5, trim = FALSE, alpha = .5, colour = NA)+
  #geom_point(aes(x = as.numeric(round)-.15, y = output, colour = pi),position = position_jitter(width = .05), size = .25, shape = 20)+
  #geom_boxplot(aes(x = round, y = output, fill = pi),outlier.shape = NA, alpha = .5, width = .1, colour = "black")+
  geom_line(data = sumrepdat, aes(x = round, y = output_mean, group = cam, colour = cam), linetype = 1)+
  geom_point(data = sumrepdat, aes(x = round, y = output_mean, group = cam, colour = cam), shape = 18) +
  geom_errorbar(data = sumrepdat, aes(x = as.numeric(round), y = output_mean, group = cam, colour = cam, ymin = output_mean-se, ymax = output_mean+se), width = .075)+
  #geom_hline(yintercept=75, linetype="dashed", color = "black") +
  geom_hline(yintercept=89.9, linetype="dashed", color = "blue") +
  geom_hline(yintercept=90.1, linetype="dashed", color = "red") +
  scale_y_continuous(breaks = seq(70, 100, 5)) +
  coord_cartesian(ylim=c(70,100)) +
  theme_minimal() +
  theme(axis.title.x = element_blank()) +
  theme(legend.position="bottom") +
  ylab("Mean Emissions Levels") +
  theme(legend.title = element_blank()) +
  scale_fill_manual(values=c("#FF0000", "#0000FF")) +
  scale_color_manual(values=c("#FF0000", "#0000FF")) +
  #-scale_color_grey(name = "Info", start = 0.6, end = 0.2) +
  #scale_fill_grey(name = "Info", start = 0.6, end = 0.2)+
  ggtitle("No Information") 

p1

ggsave('output_ni.eps')
ggsave('output_ni.png')





### OUTPUT PI ####
sumrepdat2 <- summarySE(pi_data, measurevar = "output", groupvars=c("cam", "round"))

head(sumrepdat2)
str(sumrepdat2)


p2 <- ggplot(pi_data, aes(x = round, y =output, fill = cam)) +
  #geom_flat_violin(aes(fill = cam),position = position_nudge(x = 0, y = 0), adjust = 0.5, trim = FALSE, alpha = .5, colour = NA)+
  #geom_point(aes(x = as.numeric(round)-.15, y = output, colour = pi),position = position_jitter(width = .05), size = .25, shape = 20)+
  #geom_boxplot(aes(x = round, y = output, fill = pi),outlier.shape = NA, alpha = .5, width = .1, colour = "black")+
  geom_line(data = sumrepdat2, aes(x = round, y = output_mean, group = cam, colour = cam), linetype = 1)+
  geom_point(data = sumrepdat2, aes(x = round, y = output_mean, group = cam, colour = cam), shape = 18) +
  geom_errorbar(data = sumrepdat2, aes(x = as.numeric(round), y = output_mean, group = cam, colour = cam, ymin = output_mean-se, ymax = output_mean+se), width = .075)+
  geom_hline(yintercept=75, linetype="dashed", color = "blue") +
  geom_hline(yintercept=90, linetype="dashed", color = "red") +
  scale_y_continuous(breaks = seq(70, 100, 5)) +
  coord_cartesian(ylim=c(70,100)) +
  theme_minimal() +
  theme(axis.title.x = element_blank()) +
  theme(legend.position="bottom") +
  ylab("Mean Emission Levels") +
  theme(legend.title = element_blank()) +
  scale_fill_manual(values=c("#FF0000", "#0000FF")) +
  scale_color_manual(values=c("#FF0000", "#0000FF")) +
  ggtitle("Perfect Information") 

p2


ggsave('output_pi.eps')
ggsave('output_pi.png')




### REPORT NI ####

sumrepdat3 <- summarySE(ni_data, measurevar = "report", groupvars=c("cam", "round"))


p3 <- ggplot(ni_data, aes(x = round, y =report, fill = cam)) +
  #geom_flat_violin(aes(fill = cam),position = position_nudge(x = 0, y = 0), adjust = 0.5, trim = FALSE, alpha = .5, colour = NA)+
  #geom_point(aes(x = as.numeric(round)-.15, y = output, colour = pi),position = position_jitter(width = .05), size = .25, shape = 20)+
  #geom_boxplot(aes(x = round, y = output, fill = pi),outlier.shape = NA, alpha = .5, width = .1, colour = "black")+
  geom_line(data = sumrepdat3, aes(x = round, y = report_mean, group = cam, colour = cam), linetype = 1)+
  geom_point(data = sumrepdat3, aes(x = round, y = report_mean, group = cam, colour = cam), shape = 18) +
  geom_errorbar(data = sumrepdat3, aes(x = as.numeric(round), y = report_mean, group = cam, colour = cam, ymin = report_mean-se, ymax = report_mean+se), width = .075)+
  #geom_hline(yintercept=75, linetype="dashed", color = "black") +
  geom_hline(yintercept=64, linetype="dashed", color = "blue") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  scale_y_continuous(breaks = seq(0, 100, 10)) +
  coord_cartesian(ylim=c(0,70)) +
  theme_minimal() +
  theme(axis.title.x = element_blank()) +
  theme(legend.position="bottom") +
  ylab("Mean Reported Emission Levels") +
  theme(legend.title = element_blank()) +
  scale_fill_manual(values=c("#FF0000", "#0000FF")) +
  scale_color_manual(values=c("#FF0000", "#0000FF")) +
  #-scale_color_grey(name = "Info", start = 0.6, end = 0.2) +
  #scale_fill_grey(name = "Info", start = 0.6, end = 0.2)+
  ggtitle("No Information") 

p3


ggsave('report_ni.eps')
ggsave('report_ni.png')



### REPORT PI ####

sumrepdat4 <- summarySE(pi_data, measurevar = "report", groupvars=c("cam", "round"))


p4 <- ggplot(pi_data, aes(x = round, y =report, fill = cam)) +
  #geom_flat_violin(aes(fill = cam),position = position_nudge(x = 0, y = 0), adjust = 0.5, trim = FALSE, alpha = .5, colour = NA)+
  #geom_point(aes(x = as.numeric(round)-.15, y = output, colour = pi),position = position_jitter(width = .05), size = .25, shape = 20)+
  #geom_boxplot(aes(x = round, y = output, fill = pi),outlier.shape = NA, alpha = .5, width = .1, colour = "black")+
  geom_line(data = sumrepdat4, aes(x = round, y = report_mean, group = cam, colour = cam), linetype = 1)+
  geom_point(data = sumrepdat4, aes(x = round, y = report_mean, group = cam, colour = cam), shape = 18) +
  geom_errorbar(data = sumrepdat4, aes(x = as.numeric(round), y = report_mean, group = cam, colour = cam, ymin = report_mean-se, ymax = report_mean+se), width = .075)+
  #geom_hline(yintercept=75, linetype="dashed", color = "black") +
  geom_hline(yintercept=11, linetype="dashed", color = "blue") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  scale_y_continuous(breaks = seq(0, 100, 10)) +
  coord_cartesian(ylim=c(0,70)) +
  theme_minimal() +
  theme(axis.title.x = element_blank()) +
  theme(legend.position="bottom") +
  ylab("Mean Reported Emission Levels") +
  theme(legend.title = element_blank()) +
  scale_fill_manual(values=c("#FF0000", "#0000FF")) +
  scale_color_manual(values=c("#FF0000", "#0000FF")) +
  ggtitle("Perfect Information") 

p4


ggsave('report_pi.eps')
ggsave('report_pi.png')



figure <- ggarrange(p1, p2, p3, p4,
                   #labels = c("A", "B"),
                   ncol = 2, nrow = 2,
                   common.legend = TRUE, legend = "bottom")

figure

ggsave(figure, height = 7, width = 10, filename = 'main_results.pdf')